package com.kukudexin.time.mayikt.stream;

import com.kukudexin.time.mayikt.entity.UserEntity;

import java.util.ArrayList;
import java.util.Optional;
import java.util.function.BinaryOperator;

/**
 * @author: 酷酷的鑫
 * @Date: 2022/1/12 13:41
 * @description:
 */
public class ReduceLambdaTest {
    public static void main(String[] args) {
        ArrayList<UserEntity> userEntities = new ArrayList<>();
        userEntities.add(new UserEntity("mayikt", 20));
        userEntities.add(new UserEntity("meite", 28));
        userEntities.add(new UserEntity("zhangsan", 35));
        userEntities.add(new UserEntity("liusi", 21));
        userEntities.add(new UserEntity("wangmazi", 19));
        userEntities.add(new UserEntity("xiaoke", 19));
        userEntities.add(new UserEntity("xiaoan", 21));
        Optional<UserEntity> sum = userEntities.stream().reduce(new BinaryOperator<UserEntity>() {
            @Override
            public UserEntity apply(UserEntity user1, UserEntity user2) {
                UserEntity userEntity = new UserEntity("sum", user1.getAge() + user2.getAge());
                return userEntity;
            }
        });

        Optional<UserEntity> sum2 = userEntities.stream().reduce((user1, user2) -> {
            UserEntity userEntity = new UserEntity("sum", user1.getAge() + user2.getAge());
            return userEntity;
        });
        Integer res = userEntities.stream().map(UserEntity::getAge).reduce(Integer::sum).get();
        System.out.println(res);
        System.out.println(sum.get().getAge());

    }
}
